package lumis.service.pagepersonalization.bean;

import lumis.portal.manager.ManagerFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.NoResultException;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

/**
 * Bean that represents a holder configuration (default or temporary).
 *
 * @version $Revision: 13092 $ $Date: 2011-05-28 18:19:06 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
@Entity
@Table(name = "lum_PPWidgetTmpDef")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@NamedQueries
(
		@NamedQuery(name = PagePersonalizationHolderConfiguration.FIND_BY_WIDGET_CONTENT_ID, query = "select c from PagePersonalizationHolderConfiguration c where c.widgets like :wci")
)
public class PagePersonalizationHolderConfiguration
{
	@Transient
	static final String FIND_BY_WIDGET_CONTENT_ID = "findHolderConfigurationByWidgetContentId";

	@Id
	private PagePersonalizationHolderConfigurationPK id;
	private String widgets;

	public void setId(PagePersonalizationHolderConfigurationPK id)
	{
		this.id = id;
	}

	public PagePersonalizationHolderConfigurationPK getId()
	{
		return id;
	}

	public String getWidgets()
	{
		return widgets;
	}

	public void setWidgets(String widgets)
	{
		this.widgets = widgets;
	}

	public void setWidgets(String[] widgets)
	{
		if (widgets != null && widgets.length > 0)
		{
			List<String> widgetsList = new ArrayList<String>(widgets.length);
			Collections.addAll(widgetsList, widgets);
			setWidgets(widgetsList);
		}
		setWidgets((String) null);
	}

	public void setWidgets(List<String> widgets)
	{
		if (widgets != null && widgets.size() > 0)
		{
			StringBuilder sb = new StringBuilder(widgets.size() * 33);
			for (int i = 0; i < widgets.size(); i++)
			{
				if (widgets.get(i) == null || "".equals(widgets.get(i)))
					continue;
				sb.append(widgets.get(i));
				if (i != widgets.size() - 1)
					sb.append(",");
			}
			setWidgets(sb.toString());
		}
		else
			setWidgets((String) null);
	}

	public String[] expand()
	{
		if(widgets != null)
			return widgets.split(",");
		return new String[] {};
	}

	public static PagePersonalizationHolderConfiguration getByTypeAndHolderInstanceId(Type type, String holderInstanceId)
	{
		try
		{
			return (PagePersonalizationHolderConfiguration) ManagerFactory.getEntityManager().createQuery(
					"select c from PagePersonalizationHolderConfiguration c where c.id.holderInstanceId = :id and c.id.typeOfConf = :typeOfConf")
					.setParameter("id", holderInstanceId).setParameter("typeOfConf", type.sqlType).getSingleResult();
		}
		catch (NoResultException e)
		{
			return null;
		}
	}

	public static enum Type
	{
		Default("D"), Temporary("T");
		private final String sqlType;

		private Type(String sqlType)
		{
			this.sqlType = sqlType;
		}

		public String getSqlType()
		{
			return sqlType;
		}
	}

	@SuppressWarnings("unchecked")
	public static List<PagePersonalizationHolderConfiguration> getByWidgetContentId(String widgetContentId)
	{
		return ManagerFactory.getEntityManager().createNamedQuery(FIND_BY_WIDGET_CONTENT_ID).setParameter("wci",
				"%" + widgetContentId + "%").getResultList();
	}
}
